package com.sumer.psychology.sharing.resource;

import com.sumer.psychology.common.application.ApplicationServiceRegistry;
import com.sumer.psychology.common.resource.AbstractResource;
import com.sumer.psychology.sharing.application.command.ChangeSharedResourceCommand;
import com.sumer.psychology.sharing.application.command.CreateSharedResourceCommand;
import com.sumer.psychology.sharing.application.command.DeleteSharedResourceCommand;
import com.sumer.psychology.sharing.application.command.RecommendResourceCommand;
import com.sumer.psychology.sharing.domain.model.SharedResourceId;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

/**
 * 分享rest接口
 * <p/>
 * Created by zhu on 14-9-28.
 */
@Path("/v1/sharing/")
@Consumes(MediaType.APPLICATION_JSON)
public class SharingResource extends AbstractResource {
    @Context
    private UriInfo uriInfo;

    /**
     * 新建一个分享
     *
     * @param aCreateSharedResourceCommand command
     * @return response
     */
    @POST
    public Response addSharedResource(CreateSharedResourceCommand aCreateSharedResourceCommand) {
        checkAuthorAndRole(aCreateSharedResourceCommand);

        SharedResourceId sharedResourceId = ApplicationServiceRegistry
                .sharedResourceApplicationService()
                .newSharedResource(aCreateSharedResourceCommand);

        return Response
                .status(Response.Status.CREATED)
                .location(uriInfo.getAbsolutePath())
                .entity(sharedResourceId.identity())
                .build();
    }

    /**
     * 修改一个分享
     *
     * @param sharedResourceId             id
     * @param aChangeSharedResourceCommand command
     * @return response
     */
    @PUT
    @Path("{sharedResourceId}")
    public Response changeSharedResource(
            @PathParam("sharedResourceId") String sharedResourceId,
            ChangeSharedResourceCommand aChangeSharedResourceCommand) {

        ApplicationServiceRegistry
                .sharedResourceApplicationService()
                .changeResource(aChangeSharedResourceCommand);

        return Response.status(Response.Status.ACCEPTED).build();
    }

    /**
     * 删除一个分享
     *
     * @param sharedResourceId id
     * @return response
     */
    @DELETE
    @Path("{sharedResourceId}")
    public Response deleteSharedResource(@PathParam("sharedResourceId") String sharedResourceId) {
        ApplicationServiceRegistry
                .sharedResourceApplicationService()
                .deleteResource(new DeleteSharedResourceCommand(sharedResourceId));

        return Response.status(Response.Status.ACCEPTED).build();
    }

    /**
     * 推荐分享给好友
     *
     * @param sharedResourceId resource id
     * @param friendId         friend id
     * @return response
     */
    @PUT
    @Path("{sharedResourceId}/friendId/{friendId}")
    public Response recommendResource(
            @PathParam("sharedResourceId") String sharedResourceId,
            @PathParam("friendId") String friendId) {

        ApplicationServiceRegistry
                .sharedResourceApplicationService()
                .recommend(new RecommendResourceCommand(
                        sharedResourceId,
                        friendId));

        return Response.status(Response.Status.ACCEPTED).build();
    }

    /**
     * 根据关键字查询
     *
     * @param keyword      keyword
     * @param countPerPage count per page
     * @param offset       offset
     * @return json
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response findByKeyword(
            @QueryParam("keyword") String keyword,
            @QueryParam("countPerPage") int countPerPage,
            @QueryParam("offset") int offset) {
        String json = ApplicationServiceRegistry
                .sharedResourceQueryApplicationService()
                .findByKeyword(keyword, countPerPage, offset);

        return response(json);
    }

    /**
     * 根据作者查询
     *
     * @param authorId     author id
     * @param keyword      keyword
     * @param countPerPage count per page
     * @param offset       offset
     * @return json
     */
    @GET
    @Path("author/{authorId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findByAuthor(
            @PathParam("authorId") String authorId,
            @QueryParam("keyword") String keyword,
            @QueryParam("countPerPage") int countPerPage,
            @QueryParam("offset") int offset) {
        String json = ApplicationServiceRegistry
                .sharedResourceQueryApplicationService()
                .findByAuthor(authorId, countPerPage, offset, keyword);

        return response(json);
    }

    /**
     * 查找热门
     *
     * @param countPerPage count per page
     * @return json
     */
    @GET
    @Path("topics")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findTopics(@QueryParam("countPerPage") int countPerPage) {
        String json = ApplicationServiceRegistry
                .sharedResourceQueryApplicationService()
                .displayTopics(countPerPage);

        return response(json);
    }

    /**
     * 查找唯一分享
     *
     * @param sharedResourceId resource id
     * @return json
     */
    @GET
    @Path("{sharedResourceId}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findOne(@PathParam("sharedResourceId") String sharedResourceId) {
        String json = ApplicationServiceRegistry
                .sharedResourceQueryApplicationService()
                .findById(sharedResourceId);

        return response(json);
    }
}
